package com.ljf.eslab.lab.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljf.eslab.lab.annotation.NeedSid;
import com.ljf.eslab.lab.annotation.NeedTid;
import com.ljf.eslab.lab.entity.UserRoleEntity;
import com.ljf.eslab.lab.service.StudentService;
import com.ljf.eslab.lab.service.TeacherService;
import com.ljf.eslab.lab.service.UserRoleService;
import com.ljf.eslab.lab.service.UserTokenService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

@Component
@Slf4j
@Aspect
public class IdAndRoleAspect {
    @Autowired
    HttpServletRequest request;

    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private ObjectMapper mapper;

    @Autowired
    private UserTokenService userTokenService;

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;

    @Around("@annotation(redAn)")
    public Object getSid(ProceedingJoinPoint joinPoint, NeedSid redAn) throws Throwable {
        //1.查询缓存中是否右sid，缓存中sid是SidMap的field值为token的值
        //获取request中的token
        String token = request.getHeader("token");
        Integer sid = (Integer) template.opsForHash().get("sidMap", token);
        //如果sid存在则返回
        if (sid != null && !sid.equals("")) {
            request.setAttribute("sid", sid);
            return joinPoint.proceed();
            //如果不存在则查询数据库
        } else {
            System.out.printf("--------------");
            log.debug("拿到了token:{}", token);
            if (!StringUtils.isEmpty(token)) {
                //1. 获取token对应userId
                Long idByToken = userTokenService.getIdByToken(token);
                request.setAttribute("userId", idByToken);
                log.debug("拿到了userId:{}", idByToken);
                //2. 找到userid对应sid
                sid = studentService.getIdByUserId(idByToken);
                log.debug("teacherID:{}", sid);
                //3. reqeust上设置sid
                if (sid != null) {
                    request.setAttribute("sid", sid);
                    log.debug("拿到了studentId:{}", request.getAttribute("sid"));
                }
            }
        }
        return joinPoint.proceed();
    }
    @Around("@annotation(redAn)")
    public Object getTid(ProceedingJoinPoint joinPoint, NeedTid redAn) throws Throwable {
        //1.查询缓存中是否右tid，缓存中tid是SidMap的field值为token的值
        //获取request中的token
        String token = request.getHeader("token");
        Integer tid = (Integer) template.opsForHash().get("tidMap", token);
        //如果tid存在则返回
        if (tid != null && !tid.equals("")) {
            request.setAttribute("tid", tid);
            return joinPoint.proceed();
            //如果不存在则查询数据库
        } else {
            System.out.printf("--------------");
            log.debug("拿到了token:{}", token);
            if (!StringUtils.isEmpty(token)) {
                //1. 获取token对应userId
                Long idByToken = userTokenService.getIdByToken(token);
                request.setAttribute("userId", idByToken);
                log.debug("拿到了userId:{}", idByToken);
                //2. 找到userid对应tid
                tid = teacherService.getIdByUserId(idByToken);
                log.debug("teacherID:{}", tid);
                //3. reqeust上设置tid
                if (tid != null) {
                    request.setAttribute("tid", tid);
                    log.debug("拿到了studentId:{}", request.getAttribute("tid"));
                }
            }
        }
        return joinPoint.proceed();
    }
}
